package com.ravi.util.jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtils {
	private static final String Sign = "~(K!@$$#&*dg|{nx2e";

	/**
	 * 生成token head.payload.sign
	 */
	public static String getAccessToken(Map<String,String> map){
		Calendar instance = Calendar.getInstance();
		instance.add(Calendar.DATE, 5);
		JWTCreator.Builder builder = JWT.create();
		map.forEach(builder::withClaim);
		return builder.withExpiresAt(instance.getTime())
				.sign(Algorithm.HMAC256(Sign));
	}

	/**
	 * 获得永久Token
	 */
	public static String getPerpetualToken(Map<String,String> map){
		JWTCreator.Builder builder = JWT.create();
		map.forEach(builder::withClaim);
		return builder
				.sign(Algorithm.HMAC256(Sign));
	}

	/**
	 * 验证token合法性
	 */
	public static DecodedJWT verify(String token){
		return  JWT.require(Algorithm.HMAC256(Sign)).build().verify(token);
	}

	/**
	 * 获取token中的id
	 */
	public static Integer getId(String token){
		DecodedJWT decodedJWT = verify(token);
		return Integer.parseInt(decodedJWT.getClaim("id").asString());
	}

	public static Integer getAreaId(String token) {
		DecodedJWT decodedJWT = verify(token);
		return Integer.parseInt(decodedJWT.getClaim("areaId").asString());
	}

	public static Integer getWorkId(String token) {
		DecodedJWT decodedJWT = verify(token);
		return Integer.parseInt(decodedJWT.getClaim("workId").asString());
	}

	public static Integer getRoomId(String token) {
		DecodedJWT decodedJWT = verify(token);
		return Integer.parseInt(decodedJWT.getClaim("roomId").asString());
	}
}